package com.atguigu.gmall.item.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.atguigu.gmall.list.client.ListClientFeign;
import com.atguigu.gmall.model.product.BaseCategoryView;
import com.atguigu.gmall.model.product.SkuInfo;
import com.atguigu.gmall.model.product.SpuSaleAttr;
import com.atguigu.gmall.product.client.ProductClientFeign;
import com.sun.webkit.LoadListenerClient;
import feign.Feign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * @Author: lx
 * @Time: 15:56
 * @Description:
 */
@RestController
@RequestMapping("/api/item")
public class ItemApiController {

    @Autowired
    private ProductClientFeign productClientFeign;
    @Autowired  // 1：通过类型注入 2：通过名称注入
    private ThreadPoolExecutor threadPoolExecutor;
    @Autowired
    private ListClientFeign listClientFeign;
    //数据汇总
    @GetMapping("/{skuId}")
    public Map<String,Object> getItem(@PathVariable(name = "skuId") Long skuId){
        Map<String,Object> result = new HashMap<>();
        //5部分数据
        CompletableFuture<SkuInfo> skuInfoCompletableFuture = CompletableFuture.supplyAsync(() -> {
            //返回结果是给下一个线程所使用
            //1:库存信息
            SkuInfo skuInfo = productClientFeign.getSkuInfo(skuId);
            result.put("skuInfo", skuInfo);
            return skuInfo;
        },threadPoolExecutor);

        CompletableFuture<Void> completableFutureCategoryView = skuInfoCompletableFuture.thenAcceptAsync((skuInfo -> {
            //2 ：一二三级分类
            BaseCategoryView categoryView = productClientFeign.getCategoryView(skuInfo.getCategory3Id());
            result.put("categoryView", categoryView);
        }), threadPoolExecutor);

        CompletableFuture<Void> completableFuturePrice = CompletableFuture.runAsync(() -> {
            //3：价格
            BigDecimal price = productClientFeign.getPrice(skuId);
            result.put("price", price);
        }, threadPoolExecutor);

        CompletableFuture<Void> completableFutureSpuSaleAttrListCheckBySku = skuInfoCompletableFuture.thenAcceptAsync((skuInfo -> {
            //4:销售属性及属性值 及选中
            List<SpuSaleAttr> spuSaleAttrListCheckBySku = productClientFeign.
                    getSpuSaleAttrListCheckBySku(skuInfo.getId(), skuInfo.getSpuId());
            result.put("spuSaleAttrList", spuSaleAttrListCheckBySku);
        }), threadPoolExecutor);

        CompletableFuture<Void> valuesSkuJsonCompletableFuture = skuInfoCompletableFuture.thenAcceptAsync((skuInfo -> {
            //5：进行选择时  组合 对应 库存ID
            Map<String, String> skuValueIdsMap = productClientFeign.getSkuValueIdsMap(skuInfo.getSpuId());
            result.put("valuesSkuJson", JSON.toJSONString(skuValueIdsMap));
        }), threadPoolExecutor);
        //更新热度
        CompletableFuture.runAsync(() -> {
            listClientFeign.hotScore(skuId);
        },threadPoolExecutor);

        //等待所有
        CompletableFuture.allOf(skuInfoCompletableFuture,completableFutureCategoryView
        ,completableFuturePrice,completableFutureSpuSaleAttrListCheckBySku
        ,valuesSkuJsonCompletableFuture).join();



        return result;
    }

}
